package com.yao.club.controller.goods;

import com.yao.club.entity.dto.SysAddressCity;
import com.yao.club.entity.dto.SysAddressDistrict;
import com.yao.club.entity.dto.SysAddressProvince;
import com.yao.club.entity.dto.UserAddress;
import com.yao.club.entity.request.AddUserAddressRequestEntity;
import com.yao.club.entity.request.UpdateUserAddressRequestEntity;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.service.UserAddressService;
import com.yao.club.token.UserToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "俱乐部商城-地址管理")
@RestController
@RequestMapping("/mall/userAddress")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;
    @Resource
    HttpServletRequest request;


    @UserToken
    @ApiOperation(value = "查询地址列表")
    @PostMapping("/list")
    public ResponseEntity<List<UserAddress>> memberShopAddressList() {
        String username = (String) request.getAttribute("username");
        return userAddressService.getUserAddresses(username);
    }

    @UserToken
    @ApiOperation(value = "查询所有省份")
    @GetMapping("/provinces")
    public ResponseEntity<List<SysAddressProvince>> getAllProvinces() {
        return userAddressService.getAllProvinces();
    }

    @UserToken
    @ApiOperation(value = "根据省份ID查询城市列表")
    @GetMapping("/byProvince/{provinceId}")
    public ResponseEntity<List<SysAddressCity>> getCitiesByProvinceId(@PathVariable String provinceId) {
        return userAddressService.getCitiesByProvinceId(provinceId);
    }

    @UserToken
    @ApiOperation(value = "根据城市ID查询区域列表")
    @GetMapping("/byCity/{cityId}")
    public ResponseEntity<List<SysAddressDistrict>> getDistrictsByCityId(@PathVariable String cityId) {
        return userAddressService.getDistrictsByCityId(cityId);
    }

    @UserToken
    @ApiOperation(value = "添加收货地址")
    @PostMapping("/add")
    public ResponseEntity<Boolean> addUserAddress(@RequestBody AddUserAddressRequestEntity entity) {
        String username = (String) request.getAttribute("username"); // Assuming you have a way to get the username
        return userAddressService.addUserAddress(entity, username);
    }
    @UserToken
    @ApiOperation(value = "修改收货地址")
    @PostMapping("/update")
    public ResponseEntity<Boolean> updateUserAddress(@RequestBody UpdateUserAddressRequestEntity entity) {
        String username = (String) request.getAttribute("username"); // Assuming you have a way to get the username
        return userAddressService.updateUserAddress(entity, username);
    }

    @UserToken
    @ApiOperation(value = "删除收货地址")
    @DeleteMapping("/delete/{addressId}")
    public ResponseEntity<Boolean> deleteUserAddress(@PathVariable Long addressId) {
        String username = (String) request.getAttribute("username"); // Assuming you have a way to get the username
        return userAddressService.deleteUserAddress(addressId, username);
    }
}

